package dialog

import (
	"context"

	"paper-pro/app/chat/api/internal/svc"
	"paper-pro/app/chat/api/internal/types"
	"paper-pro/pkg/ctxdata"
	"paper-pro/pkg/rpc/chat/client/chatdialogservice"

	"github.com/jinzhu/copier"
	"github.com/pkg/errors"
	"github.com/zeromicro/go-zero/core/logx"
)

type DeleteLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// delete chat dialog
func NewDeleteLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteLogic {
	return &DeleteLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *DeleteLogic) Delete(req *types.DialogDeleteReq) (resp *types.ChatDialog, err error) {
	userId, err := ctxdata.GetUidFromCtx(l.ctx)
	if err != nil {
		return nil, errors.Wrapf(err, "Chat Dialogs Delete: get UserId error: %v", err)
	}

	rpcResp, err := l.svcCtx.ChatDialogRpc.Delete(l.ctx, &chatdialogservice.ChatDialogDeleteReq{
		Id:     req.Id,
		UserId: userId,
	})
	if err != nil {
		return nil, errors.Wrapf(err, "Chat Dialogs Delete: ChatDialogRpc Delete error: %v", err)
	}

	resp = &types.ChatDialog{}
	copier.Copy(resp, rpcResp.Dialog)

	return
}
